require('./filter');
var _ = require('lodash');
var Router = require('koa-router');
var service = require('./service');
var router = module.exports = new Router({
  prefix: '/room'
});

// 收款方式
router.get('/cashier', loginRequired, function * (next) {
  var data = yield service.getProcessedRecord(this, {
    id: this.query.id
  });
  
  this.body = this.render('templates/cashier.html', this.query, {
    'totalFee': data.entity.totalFee
  })
});

// 收款二维码
router.get('/cashier/paycode/:id', loginRequired, function * (next) {
  var data = yield service.getQrcodeByApplyId(this, {
    applyId: this.params.id
  })

  this.body = this.render('templates/paycode.html', this.params, data.qrcodeVo);
});

// 是否扫码支付成功
router.get('/ajax/bepaid/:qrCodeId', loginRequired, function * (next) {
  var data = yield service.judeOrderIsPay(this, {
    qrCodeId: this.params.qrCodeId
  })

  this.body = this.render(200, data)
});

// 收款成功
router.get('/paysuccess', loginRequired, function * (next) {
  this.body = this.render('templates/paysuccess.html', this.query)
});

// 现金收款
router.get('/cashPay', loginRequired, function * (next) {

  var puserId = '';
  this.session.partners.forEach(function (user) {
    if (user.partnerType == 'bs') {
      return puserId = user.partnerId
    }
  });
  var data = yield service.cashPay(this, {
    orderCode: this.query.orderCode,
    puserId: puserId,
    paySourceType: 2
  });

  if (data.fail) {
    this.body = this.render(500, data)
  }
  else {
    this.body = this.render(200, data)
  }
});
